package com.clickpaas.workinghours.service.Impl;

import com.bizcloud.ipaas.tbba4bf83484b4ed2aacba581fbfeac4d.  d20210220155135.model.WBSDTO;
import com.bizcloud.ipaas.tbba4bf83484b4ed2aacba581fbfeac4d.  d20210220155135.model.WBSDTOResponse;
import com.clickpaas.workinghours.dao.WbsDao;
import com.clickpaas.workinghours.entity.WBSEntity;
import com.clickpaas.workinghours.service.WbsService;
import com.clickpaas.workinghours.utils.DataInfoUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class WbsServiceImpl implements WbsService {
    @Resource
    private WbsDao wbsDao;

    /**
     * 获取数据
     *
     * @param entity
     * @return
     */
    @Override
    public List<WBSDTO> getDataInfo(WBSEntity entity) {

        List<WBSDTO> reList;
        try {
            reList = DataInfoUtil.castListCopy(wbsDao.queryDataInfo(entity), WBSDTOResponse.class, WBSDTO.class);
        } catch (Exception e) {
            log.info("入参对象异常,请检查：【{}】,报错信息：【{}】",entity,e);
            return null;
        }
        if (null != reList && reList.size()>0){
            return reList;
        }
        return null;
    }

    /**
     * 项目数据转换为bean
     *
     * @param list
     * @return
     */
    @Override
    public List<WBSEntity> DataConver(List<WBSDTO> list) {
        return DataInfoUtil.castListCopy(list,WBSDTO.class,WBSEntity.class);
    }

    /**
     * 查询父节点
     *
     * @param wbsdto1
     * @return
     */
    @Override
    public String findParentNode(WBSDTO wbsdto1) {
        if (null == wbsdto1.getParent()){
            return wbsdto1.getName();
        }
        String parentName = "";
        String id = wbsdto1.getParent().toString();
        while (StringUtils.isNotBlank(id)){
            WBSDTOResponse wbsData = wbsDao.queryDataInfoById(id);
            if (null == wbsData || null == wbsData.getParent()){
                id = null;
                parentName = wbsData.getName();
            }else {
                id = wbsData.getParent().toString();
            }
        }
        return parentName;
    }
}
